package comment

import (
	"context"

	"mall/common/errorx"
	"mall/common/jwt"
	"mall/common/utils/pointy"
	"mall/service/backend/api/internal/svc"
	"mall/service/backend/api/internal/types"
	"mall/service/backend/common/i18n"
	"mall/service/forum/rpc/forumclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdatePostCommentLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 审核评论
func NewUpdatePostCommentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdatePostCommentLogic {
	return &UpdatePostCommentLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UpdatePostCommentLogic) UpdatePostComment(req *types.ForumPostCommentUpdateReq) (resp *types.BaseMsgResp, err error) {
	if len(req.Ids) == 0 {
		return nil, errorx.Error(errorx.ERROR_FORUM_POST_ID_REQUIRED, "")
	}
	userId := jwt.GetUserId(l.ctx)
	if userId == 0 {
		return nil, errorx.Error(errorx.ERROR_TOKEN_EXPIRE, "")
	}
	if req.Status == nil {
		req.Status = pointy.GetPointer(uint32(0))
	}
	_, err = l.svcCtx.ForumRpc.BackendUpdatePostComment(l.ctx, &forumclient.BackendUpdatePostCommentReq{
		Ids:       req.Ids,
		UpdatedBy: pointy.GetPointer(userId),
		Status:    req.Status,
	})
	if err != nil {
		return nil, l.svcCtx.Trans.TransError(l.ctx, err)
	}
	return &types.BaseMsgResp{
		Msg: l.svcCtx.Trans.Trans(l.ctx, i18n.UpdateSuccess),
	}, nil
}
